

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//登录
public class Login extends JPanel implements ActionListener {
    private Icon loginImage = Util.getIcon("/image/buttonLand.jpg");//登陆按钮图
    private JButton loginBtn;
    private ATM frame;//顶级容器实例
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private Font font = new Font("宋体",Font.BOLD,25);
    // 错误次数
    private int errorCount = 3;
    private JTextField iptAccount;
    private JLabel accountJLabel;
    private JPasswordField iptPassword;
    private JLabel PasswordJLabel;

    public Login(ATM frame) {
        this.frame = frame;
//        this.usersList = usersList;//也可以传集合
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //登陆按钮
        loginBtn = new JButton(loginImage);//登录按钮
        loginBtn.setBounds(870, 220, 140, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);//建立联系
        //输入账号提示插件
        accountJLabel = new JLabel("账号");
        accountJLabel.setBounds(280, 190, 100, 50);
        accountJLabel.setFont(font);
        this.add(accountJLabel);
        //输入账号框
        iptAccount = new JTextField();
        iptAccount.setBounds(380, 200, 300, 30);
        iptAccount.setFont(font);
        this.add(iptAccount);
        //输入密码提示插件
        PasswordJLabel = new JLabel("密码");
        PasswordJLabel.setBounds(280, 240, 100, 50);
        PasswordJLabel.setFont(font);
        this.add(PasswordJLabel);
        //输入密码框
        iptPassword = new JPasswordField();
        iptPassword.setBounds(380, 250, 300, 30);
        iptPassword.setFont(font);
        this.add(iptPassword);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == loginBtn){
            String account = accountTextField.getText();
            String pwd = String.valueOf(pwdField.getPassword());
            Util db = new Util();
            User user = db.getByUsernameAndPwd(account,pwd);
            if(user != null){
                frame.setLoginUser(user);
                frame.switchPanel("menu");
                return;
            }
            errorCount--;
            if(errorCount == 0){
                JOptionPane.showMessageDialog(this,"次数用完，卡被吞掉");
                System.exit(0);

            }
            JOptionPane.showMessageDialog(this,"登录失败，还剩"+(errorCount)+"次");


        }

    }
    public JPasswordField getIptPassword() {
        return iptPassword;
    }

    public JTextField getIptAccount() {
        return iptAccount;
    }
}
